package sk.stuba.fiit.pki.service.impl;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import sk.stuba.fiit.pki.dao.CertificateDao;
import sk.stuba.fiit.pki.entity.CertificateEntity;
import sk.stuba.fiit.pki.service.CertificateService;

@Service("certificateService")
public class CertificateServiceImpl implements CertificateService{

	@Autowired()
	private CertificateDao certificateDao;
	
	public Collection<CertificateEntity> findCertificates() {
		return certificateDao.findCertificates();
	}

	public void delete(CertificateEntity certificateEntity) {
		certificateDao.delete(certificateEntity);
		
	}

	public void save(CertificateEntity certificateEntity) {
		certificateDao.save(certificateEntity);
		
	}

	public CertificateEntity findCertificateBySerialNumber(BigInteger id, String issuer) {
		return certificateDao.findCertificateBySerialNumber(id,issuer);
	}
	
	
	public CertificateEntity findCertificateBySubjectIssuer(String subject) {
		return certificateDao.findCertificateBySubjectIssuer(subject);
	}

	public List<BigInteger> findRevocateCertificates() {
		List<CertificateEntity> revCert = certificateDao.findRevocateCertificate();
		List<BigInteger> result = new ArrayList<BigInteger>(revCert.size());
		for (CertificateEntity cert : revCert) {
			result.add(cert.getSerialNumber());
		}
		return result;
	}

	public void deleteAllCertificates() {
		Collection<CertificateEntity> cert  = findCertificates();
		for (CertificateEntity certificateEntity : cert) {
			delete(certificateEntity);
		}
	}

}
